package com.itheima.brand.controller;

import com.itheima.brand.service.BrandService;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.pojo.Category;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wang
 * @description
 * @date 2021/7/30 10:03
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping
    public Result findAll() {
        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",brandList);
    }

    @PostMapping
    public Result add(@RequestBody Brand brand) {
        boolean add = brandService.add(brand);
        return new Result(add, StatusCode.OK,"添加成功",null);
    }

    @GetMapping("/search/{page}/{size}")
    public Result findPage4Get(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ) {
        PageResult pageResult = brandService.findPage4Get(page,size);
        return new Result(true, StatusCode.OK,"查询成功",pageResult);
    }

    @PostMapping("/search/{page}/{size}")
    public Result findPage4Post(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size,
            @RequestBody Brand brand
    ) {
        PageResult pageResult = brandService.findPage4Post(page,size,brand);
        return new Result(true, StatusCode.OK,"查询成功",pageResult);
    }

    @GetMapping("/{id}")
    public Result findById(
            @PathVariable("id") Integer id
    ) {
        Brand brand = brandService.findById(id);
        return new Result(true, StatusCode.OK,"查询成功",brand);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(
            @PathVariable("id") Integer id
    ) {
        boolean delete = brandService.deleteById(id);
        return new Result(delete, StatusCode.OK,"删除成功",null);
    }

    @PutMapping("/{id}")
    public Result update(@RequestBody Brand brand,
                         @PathVariable("id") Integer id
    ) {
        boolean update = brandService.updateBrand(brand,id);
        return new Result(update, StatusCode.OK,"修改成功",null);
    }
}
